/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmium;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.atomic.AtomicBoolean;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Fatal;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.util.IO;

/**
 * This class defines operations for custom type "cadmium_java_object".
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public final class CustomObject extends Custom.AbstractOperations {

    /** Custom operations for "cadmium_java_object". */
    public static final Custom.Operations OPS = new CustomObject();

    /** Size for custom "cadmium_java_object" (dummy value). */
    static final int SIZE = 4;

    /**
     * Only one instance of this class.
     */
    private CustomObject() {
        super("cadmium_java_object", false, true, true);
    } // end empty constructor

    /**
     * {@inheritDoc}
     */
    @Override
    public int hash(final Value v) {
        return v.asBlock().asCustom().hashCode();
    } // end method 'hash(Value)'

    /**
     * {@inheritDoc}
     */
    @Override
    public Custom.SerializationSizes serialize(final DataOutput out,
                                               final Value v)
        throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(v.asBlock().asCustom());
        oos.close();
        final int sz = baos.size();
        IO.write32s(out, sz);
        out.write(baos.toByteArray());
        baos.close();
        return new Custom.SerializationSizes(sz, sz);
    } // end method 'serialize(DataOutput, Value)'

    /**
     * {@inheritDoc}
     */
    @Override
    public long deserialize(final DataInput in, final Block dst)
        throws IOException, Fail.Exception, Fatal.Exception {
        final int sz = IO.read32s(in);
        final byte[] b = new byte[sz];
        in.readFully(b);
        final ByteArrayInputStream bais = new ByteArrayInputStream(b);
        final ObjectInputStream ois = new ObjectInputStream(bais);
        Object obj = null;;
        try {
            obj = ois.readObject();
        } catch (final ClassNotFoundException cnfe) {
            Fail.failWith("error in Java serialization (class not found)");
        } // end try/catch
        ois.close();
        bais.close();
        dst.setCustom(obj);
        return sz;
    } // end method 'deserialize(DataInput, Block)'

} // end class 'CustomObject'
